package com.athuiju.eduservice.service.impl;


import com.athuiju.commonutils.R;
import com.athuiju.eduservice.client.OssClient;
import com.athuiju.eduservice.entity.Teacher;
import com.athuiju.eduservice.entity.vo.TeacherQuery;
import com.athuiju.eduservice.mapper.TeacherMapper;
import com.athuiju.eduservice.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author wzh
 * @since 2020-05-21
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
@Autowired
    OssClient ossClient;
    @Override
    public boolean delTeacher(String id) {
        //根据id得到老师信息
        Teacher teacher = this.baseMapper.selectById(id);
        //删除老师信息
        int i = this.baseMapper.deleteById(id);
        if (i <= 0){
            return false;
    }//得到头像
        String avatar=teacher.getAvatar();
        //对avatar处理，去除网络路径，替换斜杠
        avatar=avatar.substring(avatar.indexOf("com/")+4);//去除没有用路径，得到文件名
        //把文件中的斜杠换成下划线
        avatar=avatar.replaceAll("/","_");
        R r=ossClient.delOssFile(avatar);
        if (r!=null){
        if (r.getCode()!=20000){
            return false;
        }}
        return  true;
    }


    @Override
    public void pageQuery(Page<Teacher> pageParam, TeacherQuery teacherQusery) {
        //构造条件
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        //根据sort正序排序
        queryWrapper.orderByAsc("sort");
        //如果条件参数为空，就只根据分页查询，并return
        if ( teacherQusery==null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String name = teacherQusery.getName();
        Integer level = teacherQusery.getLevel();
        String begin = teacherQusery.getBegin();
        String end = teacherQusery.getEnd();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");


        baseMapper.selectPage(pageParam, queryWrapper);

    }


}
